import Vue from 'vue'
import Global from '@/libs/global'
import VueRouter from 'vue-router'
import { getToken,setUserBackUrl,getSharePath ,getActiveId,isNull} from '@/libs/util'
import wxApi from '@/libs/wx';
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'index',
    meta:{desc:'钻石投票',needToken:true},
    component: r => {
      require(['@/views/index/index.vue'], r)
    },
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          desc: '首页'
        },
        component: () => import('@/views/home/home.vue'),
      },
      {
        path: '/prizes',
        name: 'prizes',
        meta: {
          title: '奖品'
        },
        component: () => import('@/views/prizes/prizes.vue'),
      },
      {
        path: '/rank',
        name: 'rank',
        meta: {
          title: '排名'
        },
        component: () => import('@/views/rank/rank.vue'),
      }
    ]
  },
  {
    path: '/apply', name: 'apply',
    meta: { title: '报名',needToken:true },
    component: () => import('@/views/apply/apply.vue')
  },
  {
    path: '/comment', name: 'comment',
    meta: { desc: '评论',needToken:true },
    component: () => import('@/views/comment/comment.vue')
  },
  {
    path: '/rule', name: 'rule',
    meta: { desc: '规则',needToken:true },
    component: () => import('@/views/rule/rule.vue')
  },
  {
    path: '/player', name: 'player',
    meta: { desc: '选手',needToken:true, },
    component: () => import('@/views/player/player.vue')
  }
  ,
  {
    path: '/gift', name: 'gift',
    meta: { title: '助力',needToken:true },
    component: () => import('@/views/gift/gift.vue')
  },
  {
    path: '/report', name: 'report',
    meta: { title: '投诉',needToken:true },
    component: () => import('@/views/report/report.vue')
  },
  {
    path: '/report-notice', name: 'report-notice',
    meta: { title: '投诉须知',needToken:true },
    component: () => import('@/views/report/reportNotice.vue'),
  },
  {
    path: '/pay-result', name: 'pay-result',
    meta: { title: '支付结果',needToken:true },
    component: () => import('@/views/payresult/payResult.vue'),
  },
  {
    path: '/result', name: 'result',
    meta: { desc: '喜报' },
    component: () => import('@/views/result/result.vue'),
  },
  {
    path: '/login', name: 'login',
    component: () => import ('@/views/login/login.vue')
    ,
    meta: { title: '登录',desc:'登录'
    }
  }
]
const router = new VueRouter({
  mode: 'history',
  routes: routes
})
router.beforeEach((to, from, next) => {
  const token = getToken();
  // let main = Global.getMainData();
  // if (to.meta.title) {
  //   document.title = to.meta.title
  // }else if(!isNull(main)&&!isNull(main.title)){
  //   document.title = main.title;
  // }
  if(!token && to.meta.needToken){
    getActiveId();//如果有id的话会存起来
    setUserBackUrl(to.fullPath);
    next({
      name: 'login'
    })
  }else {
    next()
  }
})
router.afterEach((to)=>{
  // if(to.name!=='index'&&to.name!=='login'){
  //   let url = window.location.protocol + '//' + window.location.host+to.fullPath;
  //   wxApi.wxJssdkInit(url);
  //   let mainData = Global.getMainData();
  //   if(mainData!==undefined&&mainData!==null){
  //     let img = Global.getUrl(mainData.share_img !== null && mainData.share_img.length > 0 ? mainData.share_img[0] : mainData.content.banners[0].img, true)
  //     let defaultDesc= '我正在参加[activity_title]快来为我加油吧！';
  //     if(!isNull(mainData.share_desc)){
  //       defaultDesc = mainData.share_desc;
  //     }
  //     defaultDesc=defaultDesc.replace('[activity_title]',mainData.title);
  //     let shareObj = {
  //       title: mainData.title,
  //       link: getSharePath(),
  //       imgUrl: img,
  //       desc: defaultDesc
  //     };
  //     wxApi.wxShare(shareObj)
  //   }
  // }

})
export default router



